package com.atguigu.gulimail.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author Guo 2022/8/29 16:09:34
 */
@Configuration
public class GuLiMailCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfigurationSource corsConfigurationSource = new CorsConfigurationSource() {
//            @Override
//            public org.springframework.web.cors.CorsConfiguration getCorsConfiguration(ServerWebExchange serverWebExchange) {
//                return null;
//            }
//        };
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 配置跨域
        // 允许哪些请求头进行跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许哪些请求方式
        corsConfiguration.addAllowedMethod("*");
        // 允许哪些来源
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        // 任意路径都要配置跨域
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
